
package pos.control;

import java.util.ArrayList;
import pos.entity.Order;
import pos.entity.PriceAdjustment;
import pos.entity.PurchasedItem;

/**
 *
 * @author Justin Hume
 */
public class OrderControl {

    //Update the current order entity for the current list of purchased items
    //Need to check for NULL return value
    public Order updateOrderTotalPrices(Order currOrder, ArrayList<PurchasedItem> purchasedItemList) {

        //Check to ensure order has been instantiated
        if (currOrder == null) {
            currOrder = new Order();
        }

        //Ensure all purchased items are updated
        PurchasedItemControl pItemControl = new PurchasedItemControl();
        pItemControl.updatePurchasedItemPriceAttributes(purchasedItemList);

        //Sum base price, taxes, discounts and total price
        double sumBasePrices = 0;
        double sumDiscounts = 0;
        double sumTaxes = 0;
        double sumTotalPrice = 0;

        for (PurchasedItem item : purchasedItemList) {

            sumBasePrices += item.getBasePrice();
            sumDiscounts += item.getPriceAdjustedAmount();
            sumTaxes += item.getTotalTaxes();
            sumTotalPrice += item.getTotalPrice();
        }

        //Update fields on the order entity
        currOrder.setPurchasedItems(purchasedItemList);
        currOrder.setOrderTotalBasePrice(sumBasePrices);
        currOrder.setOrderTotalAdjustedAmount(sumDiscounts);
        currOrder.setOrderTotalTaxes(sumTaxes);
        currOrder.setOrderTotalPrice(sumTotalPrice);

        // ---******----- test output
/*
        System.out.println("Order base: " + currOrder.getOrderTotalBasePrice());
        System.out.println("Order taxes: " + currOrder.getOrderTotalTaxes());
        System.out.println("Order discounts: " + currOrder.getOrderTotalAdjustedAmount());
        System.out.println("Order total: " + currOrder.getOrderTotalPrice());

        for (PurchasedItem item : currOrder.getPurchasedItems()) {

            System.out.println("item Base price: " + item.getBasePrice());
            System.out.println("item Taxes: " + item.getTotalTaxes());
            System.out.println("item Discount: " + item.getPriceAdjustedAmount());
            System.out.println("item Total price: " + item.getTotalPrice());

            for (PriceAdjustment adj : item.getPriceAdjustments()) {

                System.out.println(adj.getPriceAdjustmentType() + " " + adj.getPriceAdjustmentAmount());
            }
        }
*/

        //return the updated order entity
        return currOrder;
    }
}
